<?php defined('BASEPATH') OR die('No direct access allowed.');
/**
 * Description of "Services_model"
 *
 * Created on Jan 12, 2011
 * @author Ramon Gutierrez
 * @copyright Bitstop Network Services Inc.
 * @version 0.1
 */

class Services_model extends Model {
	public function Services_model() {
		global $table;
        parent::Model();
		$this->CI =& get_instance();
		$table = 'services';
    }
	
	// Get all items
	public function getAll($client_id = false) {
		global $table;
		$query = $this->db->query("SELECT id, account_id, name, street_address, city, state, country_id, zip, contact_number FROM $table " . ($client_id ? "WHERE client_id = $client_id" : "") . " ORDER BY post_date DESC");
		return $query;
	}
	
	// Get company name of service
	public function getCompany($id) {
		$query = $this->db->query("SELECT c.name AS company FROM clients c, clients_services cs, services s WHERE s.id = " . $id . " AND c.id = cs.client_id AND s.id = cs.service_id LIMIT 1");
		$row = $query->row();
		return $row->company;
	}
	
	// Get all closing codes
	public function getClosingCodes($id) {
		$query = $this->db->select('id, name')->where('service_id', $id)->get('closing_codes');
		if($query->num_rows() > 0) {
			return $query;
		} else {
			return false;
		}
	}
	
	// Determine if company has row for a global service
	public function checkHasGlobal($id, $company) {
		$query = $this->db->select('id')->where("client_id = $company AND service_id = $id")->get('clients_services');
		return $query->num_rows();
	}
	
	// Get all global services
	public function getGlobals() {
		$query = $this->db->select('*')->where('access', '1')->get('services');
		if($query->num_rows() > 0) {
			return $query;
		} else {
			return false;
		}
	}
	
	// Get all services of company
	public function getServicesByClient($client_id, $fields = "cs.id, s.title") {
		global $table;
		$query = $this->db->select($fields)
						  ->where('cs.client_id', $client_id)
						  ->where('cs.service_id = s.id')
						  ->get('services s, clients_services cs');
		if($query->num_rows() > 0) {
			return $query;
		} else {
			return false;
		}
	}
	
	// Get count of global users
	public function globalUsers($id) {
		$query = $this->db->select('COUNT(*) AS num')->where('service_id', $id)->get('clients_services');
		$row = $query->row();
		return $row->num;
	}
	
	// Delete all associations
	public function delete2($id, $client_id = false) {
		if($client_id) {
			$query = $this->db->query("DELETE FROM clients_services WHERE service_id = $id AND client_id = $client_id");
		} else {
			$query = $this->db->delete('clients_services', array('service_id' => $id));
		}
		return $query;
	}
}

/* End of file services_model.php */
/* Location: ./system/application/models/services_model.php */